package com.CMS.client.ui.view;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.HeaderControls;
import com.smartgwt.client.types.TitleOrientation;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.HeaderControl;
import com.smartgwt.client.widgets.Window;

import com.CMS.client.CMSConstants;
import com.CMS.client.NameTokens;
import com.CMS.client.PageService;
import com.CMS.client.PageServiceAsync;
import com.CMS.client.datasource.ItemMenuDataSource;
import com.CMS.client.datasource.LanguageDataSource;
import com.axeiya.gwtckeditor.client.CKConfig;
import com.axeiya.gwtckeditor.client.CKEditor;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.CheckboxItem;
import com.smartgwt.client.widgets.form.fields.LinkItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.Layout;
import com.smartgwt.client.widgets.layout.VLayout;



public class NewPageView extends Layout
{
	private static final String DESCRIPTION = "NewPageView";	
    
    private CKEditor ckEditor;    
    private Canvas canvas = new Canvas("cnvEditor1");
    private Window winModal;
    private HeaderControl save;
    private HeaderControl close;
    
    private SelectItem menuItem;
    private TextItem pageName;
    private CheckboxItem visible;
    private SelectItem language;
    private LinkItem editButton;
    
    private Button aceptar;
    private Button cancelar;
    
    private CMSConstants constants = GWT.create(CMSConstants.class);
    
	public NewPageView()
	{
		super();
		
		ClickHandler saveHandler = new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				com.google.gwt.user.client.Window.alert(ckEditor.getHTML());				
			}
		};		
		
		ClickHandler closeHandler = new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				winModal.hide();
			}
		};
		
		save = new HeaderControl(HeaderControl.SAVE, saveHandler);
		close = new HeaderControl(HeaderControl.CLOSE, closeHandler);
		
		winModal = new Window();  
		winModal.setTitle("Editor HTML");  
		winModal.setWidth(650);  
		winModal.setHeight(440);  
		winModal.setHeaderControls(HeaderControls.HEADER_LABEL, save, close);
		winModal.setIsModal(true);  
		winModal.setShowModalMask(true);  
		winModal.centerInPage();			
		
		/*ckEditor = new CKEditor(getCkConfig());
        ckEditor.setSize("100%", "100%");
        ckEditor.setHTML("<b>Ingrese el Texto</b>");
        canvas.setWidth100();
        canvas.setHeight100();
        //canvas.setShowEdges(false);        
        canvas.addChild(ckEditor);	*/	                
		
		menuItem = new SelectItem();
		menuItem.setTitle("Item de Men&uacute;");
		
		language = new SelectItem();
		language.setTitle("Idioma");
		
		pageName = new TextItem();
		pageName.setTitle("Nombre del archivo");
		pageName.setHint("<nobr>.html</nobr>");
		
		visible = new CheckboxItem();
		visible.setTitle("Visible");
		
		editButton = new LinkItem();
		editButton.setTitle("Editar");
		editButton.setLinkTitle("HTML");
		editButton.addClickHandler(new com.smartgwt.client.widgets.form.fields.events.ClickHandler() {			
			@Override
			public void onClick(
			    com.smartgwt.client.widgets.form.fields.events.ClickEvent event) {
				canvas.clear();
				ckEditor = new CKEditor(getCkConfig());
		        ckEditor.setSize("100%", "100%");
		        ckEditor.setHTML("<b>Ingrese el Texto</b>");
		        canvas.setWidth100();
		        canvas.setHeight100();
		        //canvas.setShowEdges(false);        
		        canvas.addChild(ckEditor);
				winModal.addItem(canvas);
		        winModal.show();
			}
		});
		
		final DynamicForm form = new DynamicForm();
		form.setWidth(400);
		form.setGroupTitle(constants.newPageFormGroup());
		form.setIsGroup(true);
        form.setTitleOrientation(TitleOrientation.LEFT);   
        form.setAlign(Alignment.CENTER);
        form.setPadding(20);
        form.setFields(menuItem,language,pageName,visible,editButton);
        
        VLayout layout = new VLayout(10);
		layout.setWidth100();
		layout.setHeight100();
		layout.setLayoutMargin(10);     	
		layout.setDefaultLayoutAlign(Alignment.CENTER);	
	    layout.addMember(form);		
		
		HLayout h = new HLayout();
		h.setWidth(200);
		h.setMembersMargin(5);
		
		aceptar = new Button("Guardar");
		aceptar.setIcon("icons/16/buttonNew.png");
		aceptar.addClickHandler(new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				PageServiceAsync rpc = GWT.create(PageService.class);
				rpc.create( pageName.getValueAsString(), "HTML", 
						   Long.valueOf(language.getValue().toString()), 
						   Long.valueOf(menuItem.getValue().toString()), 
						   ckEditor.getHTML(), 
						   visible.getValueAsBoolean(),
						   Cookies.getCookie(NameTokens.USSER_SESSION),
						   new AsyncCallback<Void>() {
							@Override
							public void onFailure(Throwable caught) {
								SC.say(caught.getMessage());
							}
							@Override
							public void onSuccess(Void result) {								
								ckEditor.setHTML("");
								visible.setValue(false);
								pageName.setValue("");
								com.google.gwt.user.client.Window.alert("Alta realizada correctamente.");									
							}
						});
			}
		});
	      
		cancelar = new Button("Cancelar");
		cancelar.setIcon("icons/16/buttonCancel.png");
		
		h.addMember(aceptar);
		h.addMember(cancelar);
		layout.addMember(h);
		layout.setMargin(100);
		
		VLayout content = new VLayout();
		content.setWidth100();
		content.setHeight100();
		content.setLayoutMargin(10);  
        content.setMembersMargin(40);   
            	
		content.setAlign(Alignment.CENTER);	
		content.addMember(layout);		
		content.setBorder("2px solid #C0C3C7");
	       
		initializeComponents();
		
		this.addMember(content);
        this.draw();
	}
	
	private void initializeComponents() {
		menuItem.setOptionDataSource(new ItemMenuDataSource());
		menuItem.setAccessKey("id");
		menuItem.setValueField("id");
		menuItem.setDisplayField("description");
		
		language.setOptionDataSource(new LanguageDataSource());
		language.setAccessKey("id");
		language.setValueField("id");
		language.setDisplayField("description");
	}

	private CKConfig getCkConfig() {
        CKConfig ckConfig = new CKConfig(CKConfig.PRESET_TOOLBAR.FULL);
        //ckConfig.setWidth("100%");
        //ckConfig.setHeight("100%");
        ckConfig.setBaseFloatZIndex(1000000);
        ckConfig.setUseFormPanel(false);
        ckConfig.setResizeEnabled(true);
        return ckConfig; 
    }
	 
	 public static class Factory implements ContextAreaFactory
	 {
		 private String id;
		 
		 public Canvas create()
		 {
	      //hay que crear la vista que vamos a llamar
	      NewPageView view = new NewPageView();
		  id = view.getID();
	      return view;
	     }
		 
		 public String getID()
		 {
		   return id;
		 }

		 public String getDescription() 
		 {
		   return DESCRIPTION;
		 }
	 }
}
